<?php

namespace App\Model;

use Hyperf\Di\Annotation\Inject;
use Util\Common;
use App\Services\AdapayService;
use App\Services\FileService;
/**
 * 对公账户模型-模型
 */
class Business extends Model
{
    #[inject]
    protected AdapayService $adapayService;

    #[inject]
    protected FileService $fileService;


    const NORMAL = 0;
    const CHECKING = 1;
    const SUCCESS = 2;
    const ERROR = 3;
//    use TimeZoneTrait;

    public bool $timestamps = true;

    protected ?string $table = 'business';

    protected array $fillable = [
        'id','order_no', 'name','prov_code','area_code','social_credit_code',
        'social_credit_code_expires','business_scope','legal_person','legal_cert_id',
        'legal_cert_id_expires','legal_mp','address','zip_code','telphone',
        'email','attach_file','bank_code','bank_acct_type','card_no',
        'card_name','status','result','distributor_id','reason',
        'card_front','card_back','bank_img','member_id','bank_name','notify_time'
    ];


    protected array $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];


    public function makeAccount(){
        $files[] = $this->card_front;
        $files[] = $this->card_back;
        $files[] = $this->bank_img;
        foreach($files as &$v){
            $v = str_replace(Common::MAIN_HOST,BASE_PATH, $v);
        }
        try{
           $this->attach_file =  $zipUrl = $this->fileService->uploadZip($files);
            if (!$zipUrl) {
                return array('code'=>1001,'msg'=>'生成压缩包失败');
            }
            $adaInfo['app_id'] = $this->adapayService->appId;
            $this->order_no = $adaInfo['order_no'] = uniqid().'_'.$this->distributor_id;
            $adaInfo['member_id'] = $this->adapayService->bus_prefix.$this->distributor_id;
            $adaInfo['name'] = $this->name;
            $adaInfo['prov_code'] = $this->prov_code;
            $adaInfo['area_code'] = $this->area_code;
            $adaInfo['social_credit_code'] = $this->social_credit_code;
            $adaInfo['social_credit_code_expires'] = $this->social_credit_code_expires;
            $adaInfo['business_scope'] = $this->business_scope;
            $adaInfo['legal_person'] = $this->legal_person;
            $adaInfo['legal_cert_id'] = $this->legal_cert_id;
            $adaInfo['legal_cert_id_expires'] = (string)$this->legal_cert_id_expires;
            $adaInfo['legal_mp'] = $this->legal_mp;
            $adaInfo['address'] = $this->address;
            $adaInfo['attach_file'] = new \CURLFile($zipUrl);
            $adaInfo['notify_url'] = Common::MAIN_HOST.'/distributors/business_notify';
            if($this->member_id){
                if(!$this->account_id && $this->card_no){
                    $account_params = array(
                        "app_id"=> $this->adapayService->appId,
                        "member_id"=> $this->member_id,
                        "channel"=> "bank_account",
                        "account_info"=> [
                            "card_id" =>$this->card_no,
                            "card_name" =>$this->card_name,
                            "tel_no" => $this->legal_mp,
                            "bank_code" => $this->bank_code,
                            "prov_code" => $this->prov_code,
                            "area_code" =>$this->area_code,
                            "bank_acct_type" => "1"
                        ]
                    );
                    $createResult = $this->adapayService->settleAccount($account_params);
                    if($createResult['code'] != 200){
                        return array('code'=>1002,'msg'=>$createResult['data']['error_msg']);
                    }
                    $this->account_id =  $createResult['data']['id'];
                    $this->save();
                }
                $result = $this->adapayService->updateCorp($adaInfo);
            }else{
                $adaInfo['bank_code'] = $this->bank_code;
                $adaInfo['bank_acct_type'] = $this->bank_acct_type;
                $adaInfo['card_no'] = $this->card_no;
                $adaInfo['card_name'] = $this->card_name;
                $result = $this->adapayService->corpMember($adaInfo);
            }
            if(array_key_exists('code',$result)){
                if($result['code'] != 200){
                    if(array_key_exists('error_msg',$result['data'])){
                        $this->reason = $result['data']['error_msg'];
                        if($result['data']['error_msg'] == '该用户待审核或已开户'){
                            $this->member_id = $this->adapayService->bus_prefix.$this->distributor_id;
                        }
                    }else{
                        $this->reason ='提交审核失败 请核准内容重新提交';
                    }
                }else{
                    if($result['data']['status'] == 'succeeded'){
                        $this->status = self::SUCCESS;
                        $this->reason = '成功';
                        $this->member_id = $result['data']['member_id'];
                    }else{
                        $this->status = self::CHECKING;
                        $this->reason = '请耐心等待审核';
                    }
                }
            }
            $this->result = json_encode($result);
            $this->save();
            return array('code'=>200,'msg'=>'ok');
        }catch (\Exception $e){
            return array('code'=>1001,'msg'=>$e->getMessage());
        }
    }

}
